home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
system
/
ms_sh22b.zip
/
src
/
system.c
< prev
Wrap
C/C++ Source or Header
|
1993-12-01
|
49KB
|
2,345 lines
/* MS-DOS System Function with Swaping - system (3C)
*
* MS-DOS System - Copyright (c) 1990,1,2 Data Logic Limited.
*
* This code is subject to the following copyright restrictions:
*
* 1. Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice is duplicated in the
* source form.
*
* Author:
* Ian Stewartson
* Data Logic, Queens House, Greenhill Way
* Harrow, Middlesex HA1 1YR, UK.
* istewart@datlog.co.uk or ukc!datlog!istewart
*
* $Header: /usr/users/istewart/src/shell/sh2.2/RCS/system.c,v 2.4 1993/08/25 16:04:22 istewart Exp $
*
* $Log: system.c,v $
* Revision 2.4 1993/08/25 16:04:22 istewart
* Add support for new options
*
* Revision 2.3 1993/06/14 10:59:58 istewart
* More changes for 223 beta
*
* Revision 2.2 1993/06/02 09:52:35 istewart
* Beta 223 Updates - see Notes file
*
* Revision 2.1 1993/01/26 18:35:36 istewart
* Fix OS2 version bug (missing semi-colon).
*
* Revision 2.0 1992/05/21 16:49:54 Ian_Stewartson
* MS-Shell 2.0 Baseline release
*
*
* MODULE DEFINITION:
*
* This is a version of the standard system(3c) library function. The only
* difference is that it supports swapping and MS-SHELL EXTENDED_LINE
* processing.
*
* To get the OS2 version, compile with -DOS2
*
* There are four macros which can be changed:
*
* GET_ENVIRON To get a variable value from the environment
* FAIL_ENVIRON The result on failure
* FATAL_ERROR Handle a fatal error message
* SHELL_SWITCH The command switch to the SHELL.
*
* This module replaces the standard Microsoft SYSTEM (3C) call. It should
* work with most models. It has been tested in Large and Small model.
* When you link a program using the swapper, the swapper front end
* (swap.obj) must be the first object model on the linker command line so
* that it is located immediately after the PSP. For example:
*
* link swap+x1+x2+x3+system,x1;
* or
* cl -o z1 swap.obj x1.obj x2.obj x3.obj system
*
* The location of the system object is not relevent.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <dirent.h>
#if defined (__TURBOC__)
# include <dir.h>
#endif
#ifdef DL_MAKE
# include "make.h"
#endif
#if defined(OS2) || defined (__OS2__)
# define INCL_DOSSESMGR
# define INCL_DOSMEMMGR
# define INCL_DOSPROCESS
# define INCL_WINSWITCHLIST
# define INCL_DOSERRORS
# include <os2.h>
#else
# include <dos.h>
#endif
#ifdef __OS2__
# define F_LOCAL
#else
# define F_LOCAL near
#endif
#ifndef P_WAIT
# define P_WAIT 0
#endif
/*
* Externals declared by the swapper
*/
#if !defined(OS2) && !defined (__OS2__)
extern char far cmd_line[]; /* Command line */
extern char far path_line[]; /* Process path */
extern unsigned int far SW_intr; /* interrupt pending */
extern unsigned int far SW_Blocks; /* Number of blocks to read */
extern int far SW_fp; /* File or EMS Handler */
extern unsigned int far SW_EMsize; /* Number of extend memory blks */
extern unsigned long far SW_EMstart; /* Start addr of extend mem */
#define SWAP_TO_DISK 1 /* Swap to disk */
#define SWAP_TO_Ext 2 /* Swap to extended memory */
/* Not recommended - no mgt */
#define SWAP_TO_EMS 3 /* Swap to EMS */
#define SWAP_TO_XMS 4 /* Swap to XMS */
extern unsigned int far SW_Mode; /* Type of swapping to do */
extern unsigned int far SW_EMSFrame; /* EMS Frame segment */
extern bool far SW_I23_InShell; /* In the shell */
/* Functions */
static void F_LOCAL ClearSwapFile (void);
extern int far SA_spawn (char **);
extern void (interrupt far *SW_I23_V) (void); /* Int 23 address */
extern void (far *SW_XMS_Driver) (void); /* XMS Driver Interface */
extern int far SW_XMS_Gversion (void);
extern int far SW_XMS_Allocate (unsigned int);
extern int far SW_XMS_Free (int);
extern unsigned int far SW_XMS_Available (void);
extern void interrupt far SW_Int23 (void); /* Int 23 New address */
extern void interrupt far SW_Int00 (void); /* Int 00 New address */
#endif
#define FFNAME_MAX (PATH_MAX + NAME_MAX + 3)
/*
* Convert to bool
*/
#define C2bool(c) (bool)((c) ? TRUE : FALSE)
/* Set these to the appropriate values to get environment variables. For
* make the following values work. Normally, getenv and (char *)NULL should
* be used.
*/
#ifdef DL_MAKE
#define GET_ENVIRON(p) GetMacroValue (p)
#define FAIL_ENVIRON Nullstr
#define FATAL_ERROR(a) PrintFatalError (a)
#define SHELL_SWITCH "-ec"
#define WINDOW_NAME "DL Make"
#else
#define FATAL_ERROR(a) { fputs (a, stderr); fputc ('\n', stderr); exit (1); }
#define GET_ENVIRON(p) getenv (p)
#define FAIL_ENVIRON (char *)NULL
#define SHELL_SWITCH "-c"
#ifdef TEST
#define WINDOW_NAME "TEST"
#else
#define WINDOW_NAME
#endif
#endif
/* Declarations */
/* Open in create mode for swap file */
#define O_SMASK (O_RDWR | O_CREAT | O_TRUNC | O_BINARY)
/*
* Result from FindLocationOfExecutable
*/
#define EXTENSION_NOT_FOUND 0 /* Cannot find file */
#define EXTENSION_EXECUTABLE 1 /* OS/2 or DOS .exe or .com */
/*
* Some MSDOS Swapper info
*/
#if !defined(OS2) && !defined (__OS2__)
#define CMD_LINE_MAX 127 /* Max command line length */
/* MSDOS Memory Control Block chain structure */
#pragma pack (1)
struct MCB_list {
char MCB_type; /* M or Z */
unsigned int MCB_pid; /* Process ID */
unsigned int MCB_len; /* MCB length */
};
#pragma pack ()
#define MCB_CON 'M' /* More MCB's */
#define MCB_END 'Z' /* Last MCB's */
/* Swap Mode */
#define SWAP_OFF 0x0000 /* No swapping */
#define SWAP_DISK 0x0001 /* Disk only */
#define SWAP_EXTEND 0x0002 /* Extended memory */
#define SWAP_EXPAND 0x0004 /* Expanded memory */
static int Swap_Mode = (SWAP_DISK | SWAP_EXPAND | SWAP_EXTEND);
static char *NoSwapFiles = "No Swap files";
static char *MS_emsg = "WARNING - %s Error (%x)";
static char *MS_Space = "WARNING - %s out of space";
static char *SwapFailed = "%s swap failed (%x)";
static char *Swap_File = (char *)NULL; /* Swap file */
#endif
static char *Extend_file = (char *)NULL;
#if defined (OS2) || defined (__OS2__)
#define CMD_LINE_MAX 16000
#define EXTENSION_COUNT 2
static char *Extensions [] = { "", ".exe"};
static char path_line[FFNAME_MAX]; /* Execution path */
static char FailName[NAME_MAX + PATH_MAX + 3];
#else
#define EXTENSION_COUNT 3
static char *Extensions [] = { "", ".exe", ".com"};
#endif
/*
* Word List structure
*/
typedef struct wdblock {
short w_bsize;
short w_nword;
char *w_words[1];
} Word_B;
/*
* Extract field from a line
*/
typedef struct Fields {
FILE *FP; /* File handler */
char *Line; /* Line buffer */
int LineLength; /* Line Length */
Word_B *Fields; /* ptr to the start of fields */
} LineFields;
/*
* Program type
*/
static struct ExecutableProcessing {
char *Name;
unsigned int Flags;
unsigned char FieldSep;
} ExecProcessingMode;
/* Flags set a bit to indicate program mode */
#define EP_NONE 0x000 /* Use PSP command line */
#define EP_DOSMODE 0x001 /* Use DOS mode extended line */
#define EP_UNIXMODE 0x002 /* Use UNIX mode extended line */
#define EP_NOEXPAND 0x004 /* Use -f for this command */
#define EP_ENVIRON 0x008 /* Use environ for variable */
#define EP_NOSWAP 0x010 /* Do not swap for this command */
#define EP_COMSPEC 0x020 /* Special for .bat files */
#define EP_EXPORT 0x040 /* Use -m for this command */
#define EP_CONVERT 0x080 /* Use conversion */
#define EP_NOWORDS 0x100 /* Do word expansion */
#define EP_NOQUOTE 0x200 /* No quote protection */
/*
* Missing errno values
*/
#ifndef EIO
# define EIO 105 /* I/O error */
#endif
#ifndef E2BIG
# define E2BIG 107 /* Arg list too long */
#endif
#ifndef ENOTDIR
# define ENOTDIR 120 /* Not a directory */
#endif
/*
* Common fields in EXTENDED_LINE file
*/
#define COMMON_FIELD_COUNT 5
static struct CommonFields {
char *Name;
unsigned int Flag;
} CommonFields [] = {
{ "switch", EP_CONVERT },
{